package com.rabbiter.fm.service.impl;

import com.rabbiter.fm.dao.ArticleDao;
import com.rabbiter.fm.model.ArticleModel;
import com.rabbiter.fm.service.ArticleItemService;
import com.rabbiter.fm.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleItemService {

    @Resource
    private ArticleDao articleDao;


    @Override
    public boolean addArticleItem(ArticleModel idleItemModel) {
        return articleDao.insert(idleItemModel) == 1;
    }

        /**
     * 搜索，分页 文章
     * @param page
     * @param nums
     * @return
     */
    public PageVo<ArticleModel> findArticItem(String title, int page, int nums) {
        List<ArticleModel> list=articleDao.findArticItem(title, (page - 1) * nums, nums);
        int count=articleDao.countArticleItem(title);
        return new PageVo<>(list,count);
    }

    @Override
    public ArticleModel getArticleItem(Long id) {

        ArticleModel articleModel = articleDao.selectByPrimaryKey(id);
        return articleModel;
    }

    @Override
    public boolean deleteArticle(Long id) {
        try {
            return articleDao.deleteByPrimaryKey(id) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateDianZan(Long id) {
        ArticleModel articleModel = articleDao.selectByPrimaryKey(id);

        try {
            int dianzan=articleModel.getDianzan();
            dianzan++;
            return articleDao.updateDianZan(id, (long) dianzan) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
